@@ -1,84 +0,0 @@ |
||
1 |
-require 'csv' |
|
2 |
- |
|
3 |
-module Agents |
|
4 |
- class SentimentValueAgent < Agent |
|
5 |
- description <<-MD |
|
6 |
- The SentimentValueAgent generates `good-bad` (psychological valence or happiness index), `active-passive` (arousal), and `strong-weak` (dominance) score. It will output a value between 1 and 9. Make sure the content this agent is analyzing have sufficient length to get respectable results. |
|
7 |
- |
|
8 |
- Provide a JSONPath in `content` field where content is residing and set `expected_receive_period_in_days` to the maximum number of days you would allow to be passed between events being received by this agent. |
|
9 |
- MD |
|
10 |
- |
|
11 |
- event_description <<-MD |
|
12 |
- Events look like: |
|
13 |
- { |
|
14 |
- :content => "The quick brown fox jumps over the lazy dog.", |
|
15 |
- :valence => 6.196666666666666, |
|
16 |
- :arousal => 4.993333333333333, |
|
17 |
- :dominance => 5.63 |
|
18 |
- } |
|
19 |
- MD |
|
20 |
- |
|
21 |
- default_schedule "every_1h" |
|
22 |
- |
|
23 |
- def default_options |
|
24 |
- { |
|
25 |
- :content => "$.message.text", |
|
26 |
- :expected_receive_period_in_days => 1 |
|
27 |
- } |
|
28 |
- end |
|
29 |
- |
|
30 |
- def working? |
|
31 |
- last_receive_at && last_receive_at > options[:expected_receive_period_in_days].to_i.days.ago |
|
32 |
- end |
|
33 |
- |
|
34 |
- def receive(incoming_events) |
|
35 |
- incoming_events.each do |event| |
|
36 |
- self.memory[:queue] ||= [] |
|
37 |
- self.memory[:queue] << event.payload |
|
38 |
- end |
|
39 |
- end |
|
40 |
- |
|
41 |
- def validate_options |
|
42 |
- errors.add(:base, "content and expected_receive_period_in_days must be present") unless options[:content].present? && options[:expected_receive_period_in_days].present? |
|
43 |
- end |
|
44 |
- |
|
45 |
- def sentiment_hash |
|
46 |
- anew = {} |
|
47 |
- CSV.foreach Rails.root.join('data/anew.csv') do |row| |
|
48 |
- anew[row[0]] = row.values_at(2,4,6).map {|val| val.to_f} |
|
49 |
- end |
|
50 |
- anew |
|
51 |
- end |
|
52 |
- |
|
53 |
- def sentiment_values(anew,text) |
|
54 |
- valence, arousal, dominance, freq = [0] * 4 |
|
55 |
- text.downcase.strip.gsub(/[^a-z ]/,"").split.each do |word| |
|
56 |
- if anew.has_key?(word) |
|
57 |
- valence += anew[word][0] |
|
58 |
- arousal += anew[word][1] |
|
59 |
- dominance += anew[word][2] |
|
60 |
- freq += 1 |
|
61 |
- end |
|
62 |
- end |
|
63 |
- if valence != 0 |
|
64 |
- [valence/freq, arousal/freq, dominance/freq] |
|
65 |
- else |
|
66 |
- ["Insufficient data for meaningful answer"] * 3 |
|
67 |
- end |
|
68 |
- end |
|
69 |
- |
|
70 |
- def check |
|
71 |
- if self.memory[:queue] && self.memory[:queue].length > 0 |
|
72 |
- anew = sentiment_hash |
|
73 |
- self.memory[:queue].each do |text| |
|
74 |
- if Utils.values_at(text, options[:content])[0] |
|
75 |
- content = Utils.values_at(text, options[:content])[0] |
|
76 |
- sent_values = sentiment_values anew, content |
|
77 |
- create_event :payload => {:content => content, :valence => sent_values[0], :arousal => sent_values[1], :dominance => sent_values[2]} |
|
78 |
- end |
|
79 |
- end |
|
80 |
- self.memory[:queue] = [] |
|
81 |
- end |
|
82 |
- end |
|
83 |
- end |
|
84 |
-end |